package org.refresh.tests;

import java.util.List;

import junit.framework.TestCase;

import org.refresh.core.conf.ProblemParser;
import org.refresh.core.conf.RefreshProblem;
import org.refresh.core.expr.BinaryExpression;
import org.refresh.core.expr.Cardinality;
import org.refresh.core.expr.ConstantIntegerExpression;
import org.refresh.core.expr.SourceVariableSumExpression;
import org.refresh.core.expr.TargetVariableSumExpression;
import org.refresh.core.expr.TargetVariableValueExpression;

/*******************************************************************************
 * Copyright (c) 2005 Jules White. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors: Jules White - initial API and implementation
 ******************************************************************************/
public class ProblemParserTest extends TestCase {

	public void testRequiresHandler(){
		String conf = "System { Requires:A,B,C; }";
		ProblemParser parser = new ProblemParser();
		RefreshProblem problem = parser.parseProblem(conf);
		assertTrue(problem.getRequiresMap().get("System") != null);
		assertTrue(problem.getRequiresMap().get("System") instanceof List);
		assertTrue(((List)problem.getRequiresMap().get("System")).size() == 3);
	}
	
	public void testExcludesHandler(){
		String conf = "System { Excludes:A,B,C; }";
		ProblemParser parser = new ProblemParser();
		RefreshProblem problem = parser.parseProblem(conf);
		assertTrue(problem.getExcludesMap().get("System") != null);
		assertTrue(problem.getExcludesMap().get("System") instanceof List);
		assertTrue(((List)problem.getExcludesMap().get("System")).size() == 3);
	}
	
	public void testSelectHandler(){
		String conf = "System { Select:[1..3],A,B,C; }";
		ProblemParser parser = new ProblemParser();
		RefreshProblem problem = parser.parseProblem(conf);
		assertTrue(problem.getSelectMap().get("System") != null);
		assertTrue(problem.getSelectMap().get("System") instanceof List);
		assertTrue(((List)problem.getSelectMap().get("System")).get(0) instanceof Cardinality);
		assertTrue(((List)problem.getSelectMap().get("System")).size() == 4);
	}
	
	public void testRequiredHandler(){
		String conf = "System { Required:true; }";
		ProblemParser parser = new ProblemParser();
		RefreshProblem problem = parser.parseProblem(conf);
		assertTrue(problem.getSourceMappedInstancesCountMap().get("System") != null);
		assertTrue(problem.getSourceMappedInstancesCountMap().get("System").getMin() == 1);
		assertTrue(problem.getSourceMappedInstancesCountMap().get("System").getMax() == 1);
	}
	
	public void testTargetReqHandler(){
		String conf = "System { Target:Target.isrunning = 1; }";
		ProblemParser parser = new ProblemParser();
		RefreshProblem problem = parser.parseProblem(conf);
		assertTrue(problem.getSourceFeasibilityConstraintsMap().get("System")!=null);
		assertTrue(problem.getSourceFeasibilityConstraintsMap().get("System").size() == 1);
		assertTrue(problem.getSourceFeasibilityConstraintsMap().get("System").get(0).getExpression() instanceof BinaryExpression);
		BinaryExpression expr = (BinaryExpression)problem.getSourceFeasibilityConstraintsMap().get("System").get(0).getExpression();
		assertTrue(expr.getLeftHandSide() instanceof TargetVariableValueExpression);
		assertTrue(expr.getRightHandSide() instanceof ConstantIntegerExpression);
		assertTrue(expr.getOperation().equals("="));
	}
	
	public void testGoalHandler(){
		String conf = "System { Goal:Target.RAM.Sum - Source.RAM.Sum; }";
		ProblemParser parser = new ProblemParser();
		RefreshProblem problem = parser.parseProblem(conf);

		BinaryExpression expr = (BinaryExpression)problem.getGoalFunction();
		assertNotNull(expr);
		assertTrue(expr.getLeftHandSide() instanceof TargetVariableSumExpression);
		assertTrue(expr.getRightHandSide() instanceof SourceVariableSumExpression);
		assertTrue(expr.getOperation().equals("-"));
		assertTrue(problem.isMaximizeGoal());
	}
	
	public void testDefineHandler(){
		String conf = "System { Define:A=43; }";
		ProblemParser parser = new ProblemParser();
		RefreshProblem problem = parser.parseProblem(conf);
		assertNotNull(problem.getSourceVariableValuesTable().get("System"));
		assertNotNull(problem.getSourceVariableValuesTable().get("System").get("A"));
		assertEquals(problem.getSourceVariableValuesTable().get("System").get("A"),new Integer(43));
	}
	
	public void testSetHandler(){
		String conf = "System { Set:System.platform=Linux; }";
		ProblemParser parser = new ProblemParser();
		RefreshProblem problem = parser.parseProblem(conf);
		assertNotNull(problem.getInjectionValues("System"));
		assertNotNull(problem.getInjectionValues("System").get("System.platform"));
		assertEquals(problem.getInjectionValues("System").get("System.platform"),"Linux");
	}
}
